package xyz.gzdxc.common.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import xyz.gzdxc.common.constant.AuthConstant;
import xyz.gzdxc.common.constant.ResultCode;
import xyz.gzdxc.common.mysql.entity.UserTokenEntity;
import xyz.gzdxc.common.service.UserService;
import xyz.gzdxc.common.util.CookieUtil;
import xyz.gzdxc.common.util.ResultBuilder;
import xyz.gzdxc.common.vo.response.ResultVo;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/user")
public class UserController {
    private static final Logger LOG = LoggerFactory.getLogger(UserController.class);

    @Autowired
    UserService userService;

    @RequestMapping("/getUserInfo")
    ResultVo<UserTokenEntity> getUserInfo(HttpServletRequest request){
        String session = CookieUtil.getCookieValue(request, AuthConstant.SESSION_KEY);
        if(session != null && !"".equals(session)){
            UserTokenEntity userTokenEntity = userService.getUserToken(session);
            if(userTokenEntity != null){
                return ResultBuilder.build(ResultCode.SUCCESS, userTokenEntity);
            } else {
                return ResultBuilder.build(ResultCode.INVALID_SESSION);
            }
        } else {
            return ResultBuilder.build(ResultCode.INVALID_SESSION);
        }
    }



}
